<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/8/28
 * Time: 13:46
 */

namespace app\api\controller;


use think\Db;

class Apilogin extends Base
{
    /**
     * 发送验证码
     * @return \think\response\Json|void
     * phone       填写的手机号
     */
    public function getPhoneCode()
    {
        header("Access-Control-Allow-Origin: *");
        $params = input('');
        if(trim($params['phone'], ' ') == '') {
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
        if(!preg_match("/^1[3456789]\d{9}$/", $params['phone'])) {
            return json(['status' => 0, 'msg' => '手机号格式不正确']);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $params['phone'])->find();
        if(!empty($userInfo)){
            if($userInfo['user_status'] == 3){
                return json(['status' => 0, 'msg' => '该账号已注销或已冻结']);
            }
        }
//        $code = GetRandCode(4);//4位数验证码
        $code = '1111';//4位数验证码
        $sendres = YzxSms($code, $params['phone'], '');//发送验证码
        if($sendres['code'] != '00000') {//发送不成功
            return json(['status' => 0, 'msg' => $sendres['msg']]);
        } else {
            $data = [
                'phone' => $params['phone'],//发送的手机号
                'code' => $code,//验证码
                'time' => time(),//验证码发送的时间戳
                'date_time' => date('Y-m-d H:i:s')//日期格式
            ];
            $res = Db::table('up_phonecode')->insert($data);
            if(!$res) {
                return json(['status' => 0, 'msg' => '数据库操作失败']);
            }
            return json(['status' => 200, 'msg' => $code]);
        }
    }
    
    /**
     * 注册/登录
     * @return \think\response\Json
     * phone    手机号
     * code     验证码
     */
    public function register(){
        header("Access-Control-Allow-Origin: *");
        $phone = trim(input('phone'), ' ');
        $code = trim(input('code'), ' ');
        $invite_phone = input('invite_phone');
        // 必填项
        if($phone == '' || $code == ''){
            return json(['status' => 0, 'msg' => '有未填项']);
        }
        // 手机号格式
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(!empty($user)){
            if($user['user_status'] == 3){
                return json(['status' => 0, 'msg' => '该账号已注销或已冻结']);
            }
        }
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        // 验证码超时
        $now = time();
        if($now - $res['time'] >= 1801) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $data['user_last_online_time'] = date('Y-m-d H:i:s');
        $data['user_token'] = $token;
        if($user){ // 登录
            $result = Db::table('up_u_user')->where('user_phone', $phone)->update($data);
            $code_type = 1;
        }else{
            $data['user_phone'] = $phone;
            $data['user_time'] = date('Y-m-d H:i:s');
            $data['user_uid'] = time() . rand(0000, 9999);
            if(isset($invite_phone) && $invite_phone != ''){
                $data['user_invite_phone'] = $invite_phone;
            }
            $result = Db::name('u_user')->insertGetId($data);
            $code_type = 2;
        }
        if($result){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
            //注册成功后自动登录
            $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
            if($userInfo['user_gender'] == 1){
                $userInfo['vip'] = $this->getVip($userInfo['user_id']);
            }else{
                $userInfo['vip'] = 1;
            }
            $rytoken = $this->rongyun($userInfo['user_id'],'who',$userInfo['user_image']);
            $userInfo['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $userInfo,'codeType'=>$code_type]);
        }else{
            return json(['status' => 0, 'msg' => '注册失败']);
        }
    }
    
    /**
     * 账号密码登录
     * @return \think\response\Json
     * phone   手机号
     * password   密码
     */
    public function passwordLogin(){
        $phone = trim(input('phone'), ' ');
        $password = trim(input('password'), ' ');
        if($phone == ''){
            return json(['status' => 0, 'msg' => '请输入手机号']);
        }
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        if($password == ''){
            return json(['status' => 0, 'msg' => '请输入密码']);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if(empty($userInfo)){
            return json(['status' => 0, 'msg' => '账号不存在']);
        }else{
            if($userInfo['user_password'] != md5($password)){
                return json(['status' => 0, 'msg' => '密码错误']);
            }

            $token = md5(date('YmdHis') . rand(100000, 999999));
            $data['user_last_online_time'] = date('Y-m-d H:i:s');
            $data['user_token'] = $token;
            $res = Db::table('up_u_user')->where('user_phone', $phone)->update($data);
            if($res){
                $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
                if($user['user_gender'] == 1){
                    $user['vip'] = $this->getVip($user['user_id']);
                }else{
                    $user['vip'] = 1;
                }
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
                return json(['status' => 200, 'msg' => $user]);
            }else{
                return json(['status' => 0, 'msg' => '系统异常']);
            }
        }

    }
    
    /**
     * 用于找回密码时验证手机号
     * @return \think\response\Json
     * phone    填写的手机号
     */
    public function checkPhone(){
        $phone = input('phone');
        // 必填项
        if($phone == ''){
            return json(['status' => 0, 'msg' => '请填写手机号']);
        }
        // 手机号格式
        if(!preg_match("/^1[3456789]\d{9}$/", $phone)) {
            return json(['status' => 0, 'msg' => '请输入正确的手机号']);
        }
        $have = Db::table('up_u_user')->where('user_phone', $phone)->find();
        if($have){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => '账号不存在']);
        }
    }
    
    /**
     * 验证验证码
     * @return \think\response\Json
     * phone     手机号
     * code      验证码
     */
    public function checkCode(){
        $phone  = input('phone');
        $code = input('code');
        // 必填项
        if($code == ''){
            return json(['status' => 0, 'msg' => '请输入验证码']);
        }
        //验证验证码
        $res = Db::table('up_phonecode')
            ->where('phone', $phone)
            ->order('time desc')
            ->limit(1)
            ->find();
        if($code != $res['code']) {
            return json(['status' => 0, 'msg' => '验证码错误']);
        }
        // 验证码超时
        $now = time();
        if($now - $res['time'] >= 1801) {
            return json(['status' => 0, 'msg' => '验证码超时']);
        }
        if($res['status'] == 1) {
            return json(['status' => 0, 'msg' => '验证码已使用']);
        }
        //修改验证码状态
        $res = Db::table('up_phonecode')->where('id', $res['id'])->setField('status', 1);
        if($res){
            return json(['status' => 200, 'msg' => true]);
        }else{
            return json(['status' => 0, 'msg' => '服务器异常']);
        }
    }
    
    /**
     * 设置密码
     * @return \think\response\Json
     * phone   手机号
     * password  密码
     */
    public function setPassword(){
        $phone = input('phone');
        $password = input('password');
        // 必填项
        if($password == ''){
            return json(['status' => 0, 'msg' => '请输入密码']);
        }
        $update = [
            'user_password' => md5($password),
            'user_update_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '密码设置成功']);
        }else{
            return json(['status' => 0, 'msg' => '密码设置失败']);
        }
    }
    //会员充值名额
    public function quota(){
       $quota =  Db::table('up_other')->where('other_id',11)->value('quota');
       return json(['status' => 200, 'msg' => $quota]);
    }
    //会员充值专享
    public function vip(){
     $type = input('param.type');
     $where['vip_seat'] = $type;
     $res = Db::table('up_vip')->where($where)->select();
     return json(['status' => 200, 'msg' => $res]);
    }
}